// pages/addLesson/addLesson.js
var common = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lessonName:'',
      lessonUrl:'',
      lessonIntroduction:'',
      tempFilePaths:'',
      imgUrl:'../../image/uploadImgW.png',
      imgUrl2:'../../image/add.png',
      src:'',
      cardImg:'',
      cardImg2:'',
      cardVideo:''
  },

    inputChange: function(e){
        var field = e.target.dataset.field,temp_data={}
        temp_data[field]=e.detail.value
        this.setData(temp_data)
    },
    choose: function () {
        var that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                var tempFilePaths = res.tempFilePaths;
                that.setData({
                    imgUrl: res.tempFilePaths[0]
                });
                wx.setStorage({ key: "card", data: tempFilePaths[0] })
            }
        })
    },
    choose2: function () {
        var that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                var tempFilePaths = res.tempFilePaths;
                that.setData({
                    imgUrl2: res.tempFilePaths[0]
                });
                wx.setStorage({ key: "card", data: tempFilePaths[0] })
            }
        })
    },
    removeImg: function () {
        this.setData({
            imgUrl: '../../image/uploadImgW.png'
        })
    },
    removeImg2: function () {
        this.setData({
            imgUrl2: '../../image/add.png'
        })
    },
    // chooseVideo: function () {
    //     var that = this;
    //     wx.chooseVideo({
    //         success: function (res) {
    //             that.setData({
    //                 src: res.tempFilePath
    //             });
    //         }
    //     })
    // },
    // removeVideo: function () {
    //     this.setData({
    //         src: ''
    //     })
    // },
    submit:function () {
        var that = this;
        if(!that.data.lessonName){
            common.info('请填写课程名称')
        }else if(that.data.imgUrl == '../../image/uploadImgW.png'){
            common.info('请上传课程封面')
        }else if(!that.data.lessonUrl || !/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)/.test(that.data.lessonUrl)){
            common.info('请填写以http://开头的视频地址')
        }else if(!that.data.lessonIntroduction){
            common.info('请填写课程介绍')
        }else if(that.data.imgUrl2 == '../../image/add.png'){
            common.info('请上传课程图片')
        }else {

            common.ajax({
                url: 'UploadFile/uploadFileSingle',
                data: {},
                file: {
                    name: 'image',
                    path: that.data.imgUrl
                },
                loading: '加载中...',
                userinfo: true,
                success: function (res) {
                    that.setData({
                         cardImg:JSON.parse(res).result.image_relative_path
                    });

                    common.ajax({
                        url: 'UploadFile/uploadFileSingle',
                        data: {},
                        file: {
                            name: 'image',
                            path: that.data.imgUrl2
                        },
                        loading: '加载中...',
                        userinfo: true,
                        success: function (res) {
                            that.setData({
                                cardImg2: JSON.parse(res).result.image_relative_path
                            });

                            common.ajax({
                                url: 'User/createTeacherClass',
                                data: {
                                    name: that.data.lessonName,
                                    logo: that.data.cardImg,
                                    video: that.data.lessonUrl,
                                    detail: that.data.lessonIntroduction,
                                    image: that.data.cardImg2
                                },
                                loading: '加载中...',
                                userinfo: true,
                                success: function (res) {
                                    if (res.status == 'SUCCESS') {
                                        common.info(res.result.msg)
                                        setTimeout(function () {
                                            wx.navigateBack({})
                                        }, 1500)
                                    } else {
                                        common.info(res.result.msg)
                                    }
                                }
                            })

                        }
                    })

                }
            })
        }

    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.setNavigationBarTitle({
          title: '添加课程'
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})